package com.dongdong.controller;


import com.dongdong.result.Result;
import com.dongdong.result.ResultEnums;
import com.dongdong.service.CartService;
import com.dongdong.utils.JwtUtil;
import com.dongdong.vo.CartVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private JwtUtil jwtUtil;

    //加入购物车
    @GetMapping("addCart")
    public Result addCart(@RequestParam Long skuId, @RequestParam Integer num, @RequestHeader String token){

        //解析token
        Claims claims = jwtUtil.parseJwt(token);
        String id = claims.getId();
        cartService.addCart(skuId, num,id);
        return Result.RESPONSE(ResultEnums.ADD_CART_SUCCESS);

    }
    //查询购物车
    @GetMapping("/getCart")
    public Result getCart(@RequestHeader String token){
    // 解析token
    Claims claims = jwtUtil.parseJwt(token);
    String id = claims.getId();
    List<CartVO> cartVOList = cartService.getCart(id);
    return Result.OK().setData(cartVOList);
    }

    //查询购物车2
    @GetMapping("/getCartList")
    public List<CartVO> getCartList(@RequestHeader String token){
        // 解析token
        Claims claims = jwtUtil.parseJwt(token);
        String id = claims.getId();
        List<CartVO> cartVOList = cartService.getCart(id);
        return cartVOList;
    }





}
